package com.java.springboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.springboot.mapper.Bookmapper;
import com.java.springboot.model.Book;
import com.java.springboot.service.BookserviceImpl;
import com.java.springboot.untis.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class Bookcontroller {

    @Autowired
    private BookserviceImpl bookservice;

    @RequestMapping("showbook")
    public String showbook(@RequestParam(value = "pn",defaultValue = "1")int pn,Book book,Model model,String bookname,String author){
        book.setBookname(bookname);
        book.setAuthor(author);
        PageHelper.startPage(pn,5);
        List<Book> getlist = bookservice.getif(book);
        PageInfo pageInfo = new PageInfo(getlist,5);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }


    @RequestMapping(value = "book",method = RequestMethod.GET)
    @ResponseBody
    public Msg book(@RequestParam(value = "pn",defaultValue = "1")int pn, Book book){
        PageHelper.startPage(pn,5);
        List<Book> bookList = bookservice.getif(book);
        PageInfo pageInfo = new PageInfo(bookList,5);
        return Msg.success().add("pageInfo",pageInfo);
    }

    @RequestMapping(value = "sebook",method = RequestMethod.GET)
    public String sebook(@RequestParam(value = "pn",defaultValue = "1")int pn, Book book,String bookname,String author,Model model){
        book.setBookname(bookname);
        book.setAuthor(author);
        PageHelper.startPage(pn,5);
        List<Book> getlist = bookservice.getlist(bookname,author);
        PageInfo pageInfo = new PageInfo(getlist,5);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }





}
